name: Release

on:
  push:
    tags: [ '*.*.*' ]

env:
  REGISTRY: ghcr.io

permissions:
  contents: write

jobs:
  client-docker:
    name: Client Docker Image
    runs-on: ubuntu-latest
    outputs: 
      version: ${{ steps.set-version-output.outputs.version }}
    permissions:
      contents: write
      packages: write
      id-token: write
    env:
      IMAGE_NAME: ${{ github.repository }}-client

    steps:
      - name: Get current date and time
        id: datetime
        run: echo "::set-output name=datetime::$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
  
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install cosign
        uses: sigstore/cosign-installer@e1523de7571e31dbe865fd2e80c5c7c23ae71eb4 #v3.4.0
        with:
          cosign-release: 'v2.1.1'

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@0d103c3126aa41d772a8362f6aa67afac040f80c # v3.1.0

      - name: Code Format Check (gofmt)
        run: |
          if [ -n "$(gofmt -l .)" ]; then
            echo "Go code is not formatted:"
            gofmt -d .
            exit 1
          fi

      - name: Log into registry ${{ env.REGISTRY }}
        uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        env:
          DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index

      - name: Build and push Docker image
        id: build-and-push
        uses: docker/build-push-action@af5a7ed5ba88268d5278f7203fb52cd833f66d6e # v5.2.0
        with:
          context: ./src/client/
          provenance: false
          file: ./src/client/Dockerfile
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          platforms: linux/amd64,linux/arm64/v8
          annotations: |
            ${{ steps.meta.outputs.annotations }},
            io.artifacthub.package.logo-url=https://github.com/dokulabs/.github/blob/main/profile/assets/logo-orange-bg.jpg?raw=true,
            io.artifacthub.package.readme-url=https://raw.githubusercontent.com/dokulabs/doku/main/README.md,
            io.artifacthub.package.license=Apache-2.0,
            org.opencontainers.image.vendor=Doku Labs,
            io.artifacthub.package.maintainers=[{'name':'Doku Labs','email':'developers@dokulabs.com'}],
            io.artifacthub.package.keywords='Monitoring, Observability, LLMs, OpenAI'
      
      - name: Sign the published Docker image
        env:
          TAGS: ${{ steps.meta.outputs.tags }}
          DIGEST: ${{ steps.build-and-push.outputs.digest }}
        run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}

  ingester-docker:
    name: Ingester Docker Image
    runs-on: ubuntu-latest
    outputs: 
      version: ${{ steps.set-version-output.outputs.version }}
    permissions:
      contents: write
      packages: write
      id-token: write
    env:
      IMAGE_NAME: ${{ github.repository }}-ingester

    steps:
      - name: Get current date and time
        id: datetime
        run: echo "::set-output name=datetime::$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
  
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install cosign
        uses: sigstore/cosign-installer@e1523de7571e31dbe865fd2e80c5c7c23ae71eb4 #v3.4.0
        with:
          cosign-release: 'v2.1.1'

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@0d103c3126aa41d772a8362f6aa67afac040f80c # v3.1.0

      - name: Code Format Check (gofmt)
        run: |
          if [ -n "$(gofmt -l .)" ]; then
            echo "Go code is not formatted:"
            gofmt -d .
            exit 1
          fi

      - name: Log into registry ${{ env.REGISTRY }}
        uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        env:
          DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index

      - name: Build and push Docker image
        id: build-and-push
        uses: docker/build-push-action@af5a7ed5ba88268d5278f7203fb52cd833f66d6e # v5.2.0
        with:
          context: ./src/ingester/
          provenance: false
          file: ./src/ingester/Dockerfile
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          platforms: linux/386,linux/amd64,linux/arm/v7,linux/arm64/v8,linux/ppc64le
          annotations: |
            ${{ steps.meta.outputs.annotations }},
            io.artifacthub.package.logo-url=https://github.com/dokulabs/.github/blob/main/profile/assets/logo-orange-bg.jpg?raw=true,
            io.artifacthub.package.readme-url=https://raw.githubusercontent.com/dokulabs/doku/main/README.md,
            io.artifacthub.package.license=Apache-2.0,
            org.opencontainers.image.vendor=Doku Labs,
            io.artifacthub.package.maintainers=[{'name':'Doku Labs','email':'developers@dokulabs.com'}],
            io.artifacthub.package.keywords='Monitoring, Observability, LLMs, OpenAI'
      
      - name: Sign the published Docker image
        env:
          TAGS: ${{ steps.meta.outputs.tags }}
          DIGEST: ${{ steps.build-and-push.outputs.digest }}
        run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}

      - name: Set version output
        id: set-version-output
        run: echo "::set-output name=version::${{ env.DOCKER_METADATA_OUTPUT_VERSION }}"
  
  ingester-binaries:
    name: Ingester Binaries
    needs: ingester-docker
    runs-on: ubuntu-latest
    strategy:
      matrix:
        platform:
          - {os: "linux", arch: "amd64"}
          - {os: "linux", arch: "arm64"}
          - {os: "linux", arch: "arm"}
          - {os: "linux", arch: "ppc64le"}
          - {os: "windows", arch: "amd64"}
          - {os: "windows", arch: "386"}
          - {os: "darwin", arch: "amd64"}
          - {os: "darwin", arch: "arm64"}
    
    steps:
    - uses: actions/checkout@v4

    - uses: actions/setup-go@v5
      with:
        go-version: '1.21.5'

    - name: Build package for ${{ matrix.platform.os }}-${{ matrix.platform.arch }}
      working-directory: ./src/ingester
      run: env GOOS=${{ matrix.platform.os }} GOARCH=${{ matrix.platform.arch }} go build -o ingester-${{ matrix.platform.os }}-${{ matrix.platform.arch }}

    - name: Upload binaries to release
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: src/ingester/ingester-${{ matrix.platform.os }}-${{ matrix.platform.arch }}
        asset_name: ingester-${{ matrix.platform.os }}-${{ matrix.platform.arch }}
        tag: ${{ needs.ingester-docker.outputs.version }}
        overwrite: true
